<?php
// @formatter:off
/**
 * @file c.directories.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-07 18:03:56 +0000 (Sun, 07 Jul 2013) $
 *
 * $Id: c.directories.php 91 2013-07-07 18:03:56Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/themes/default/controlers/contents/c.directories.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_DIRECTORY_LIST)) {
	require_once "{$this->_themeRoot}/controlers/generics/bundlepreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcPageCache;
	global $wctDefaults;
	global $wcDirectoriesHolder;

	$wcPageCache->anyDirectory();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minDirInPage = $wctDefaults["max-per-page"]["directoies"] * ($page - 1);
	$maxDirInPage = $minDirInPage + $wctDefaults["max-per-page"]["directoies"];

	$wcThemeAssigns["PATH"] = "All Directory (page {$page})";
	$wcThemeAssigns['ACTIONCONTENT'] = "/contents/c.directories.html";

	//
	// Selecting only directories in range.
	$directories = array();
	$dirCount = 0;
	foreach($wcDirectoriesHolder->itemIds($wcGroup) as $id) {
		$directory = $wcDirectoriesHolder->item($id, $wcGroup);
		if($directory && $directory->ok() && $directory->count > 0) {
			$dirCount++;
		} else {
			continue;
		}
		if($dirCount >= $minDirInPage && $dirCount < $maxDirInPage) {
			$directories[] = $directory;
		}
	}

	wcSetPager(requestParams(true, $wcAction, array('page')), ceil($dirCount / $wctDefaults["max-per-page"]["directoies"]), $page);

	//
	// Creating multi-array by prefix
	$lists = array();
	$ids = 1;
	foreach($directories as $directory) {
		$name = $directory->name;
		$prefix = strtoupper(isset($name[1]) ? $name[1] : "#");
		if($prefix < 'A' || $prefix > 'Z') {
			$prefix = '#';
		}
		$prefix = "/{$prefix}";
		if(!isset($lists[$prefix])) {
			$lists[$prefix] = array(
				"prefix" => $prefix,
				"list" => array()
			);
		}

		$lists[$prefix]["list"][] = wcGetDirectoryPreviewAssigns($directory);
	}

	ksort($lists);

	$wcThemeAssigns["DIRECTORIES-LISTS"] = $lists;
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to list directories";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>
